package com.gxa.saas.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.gxa.saas.pojo.RouteDetail;
import com.gxa.saas.pojo.RouteManagement;
import com.gxa.saas.pojo.Store;
import com.gxa.saas.service.RouteDetailService;
import com.gxa.saas.service.RouteManagementService;
import com.gxa.saas.service.StoreService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.RoundingMode;
import java.util.*;

@Api
@RestController
@RequestMapping("/route")
@CrossOrigin
public class RouteManagementController {



    Map map=new HashMap();
    @Autowired
    RouteManagementService routeManagementService;

    @Autowired
    RouteDetailService routeDetailService;

    @Autowired
    StoreService storeService;


    //点击路线管理之后，弹出的分页查询
    @ApiOperation(value = "分页查询",produces ="query")
    @ApiImplicitParam(name = "page",value = "页数",dataType="Integer",paramType ="query")
    @PostMapping("/selectByPage")
    public Map selectByPage(Integer page){
        System.out.println("有请求进来");
        System.out.println(page);
        Integer size=3;
        List<RouteManagement> routeManagements = routeManagementService.selectByPage(page, size);

        List<RouteManagement> routeManagements1 = routeManagementService.selectAll();

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(routeManagements);
        jsonResult.set("total",routeManagements1.size());
     //  jsonResult.set("pageSize",size);
        return jsonResult.getMap();
    }

    //路线管理界面的查询操作；

    @ApiOperation(value = "根据条件查询",produces ="query")
    @ApiImplicitParams({
            @ApiImplicitParam(name="routeName",value = "路线名称",dataType ="String",paramType ="query"),
            @ApiImplicitParam(name="brandName",value = "负责人",dataType ="String",paramType ="query")
    })
    @PostMapping("/selectByName")
    public Map selectByName(String routeName,String brandName){
        map.put("routeName",routeName);
        map.put("brandName",brandName);

        List<RouteManagement> routeManagements = routeManagementService.selectByName(map);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(routeManagements);
        return jsonResult.getMap();
    }

    //路线管理界面的删除
    @ApiOperation(value = "删除",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="Integer",paramType ="query")
    @PostMapping("/delete")
    public Map delete(Integer id){


        routeManagementService.delete(id);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }


    //前端添加截面，提交之后的执行的操作；
    @ApiOperation(value = "添加路线的操作",produces ="query")
    @PostMapping("/add")

       public Map add(){

        RouteManagement routeManagement = new RouteManagement();
        Integer add = routeManagementService.add(routeManagement);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.set("id",routeManagement.getId());
        return  jsonResult.getMap();
    }



    //添加截面，做门店添加时的访问路径
    @ApiOperation(value = "门店列表点击添加",produces ="query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sname",value = "门店名称",dataType="string",paramType ="query"),
            @ApiImplicitParam(name = "rname",value = "路线名称",dataType="string",paramType ="query"),
            @ApiImplicitParam(name = "nnumber",value = "编号",dataType="string",paramType ="query"),
    })
    @PostMapping("/insert")
    public Map insert(String sname,String rname,String nnumber){
        RouteDetail routeDetail = new RouteDetail();
        routeDetail.setNnumber(nnumber);
        routeDetail.setSname(sname);
        routeDetail.setRname(rname);

        routeDetailService.add(routeDetail);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }

    //添加截面，门店删除时访问路径；
    @ApiOperation(value = "删除门店",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="Integer",paramType ="query")
    @PostMapping("/remove")
    public Map remove(Integer id){
      routeDetailService.delete(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }


    //点击提交的时候，执行的操作；
    @ApiOperation(value = "点击提交时的操作",produces ="query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "routeName",value = "路线名称",dataType="string",paramType ="query"),
            @ApiImplicitParam(name = "brandName",value = "负责人",dataType="string",paramType ="query"),
            @ApiImplicitParam(name = "used",value = "是否启用",dataType="Boolean",paramType ="query"),
            @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query"),
    })
    @PostMapping("/update")
    public Map update(String routeName,String brandName,Integer id,Boolean used){
        RouteManagement routeManagement=new RouteManagement();
        routeManagement.setRouteName(routeName);
        routeManagement.setBrandName(brandName);
        routeManagement.setId(id);
        routeManagement.setUsed(used);
      routeManagementService.update(routeManagement);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }


    @ApiOperation(value = "点击编辑时的查询",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query")
    @PostMapping("/selectDetail")
    public Map selectDetail(Integer id){
        RouteManagement routeManagement = routeManagementService.selectByUp(id);
       // List<Store> stores = storeService.selectByPage(1, 8);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(routeManagement);
       // jsonResult.set("stores",stores);
        return  jsonResult.getMap();
    }


    @ApiOperation(value = "上移",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query")
    @PostMapping("/up")
    public Map up(Integer id,String name){
        routeDetailService.up(id,name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }

    @ApiOperation(value = "下移",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query")
    @PostMapping("/down")
    public Map down(Integer id,String name){
        routeDetailService.down(id,name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }

    @ApiOperation(value = "置顶",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query")
    @PostMapping("/first")
    public Map first(Integer id){
        routeDetailService.first(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }

    @ApiOperation(value = "置底",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query")
    @PostMapping("/last")
    public Map last(Integer id){
        routeDetailService.last(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }

    @ApiOperation(value = "删除路线的门店",produces ="query")
    @ApiImplicitParam(name = "id",value = "编号",dataType="integer",paramType ="query")
    @PostMapping("/deleteStore")
    public Map deleteStore(Integer id){
        routeDetailService.delete(id);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        return  jsonResult.getMap();
    }

    @ApiOperation(value = "门店根据条件查询",produces ="query")
    @PostMapping("/selectStore")
    public Map selectStore(String name,String type){
        Map map=new HashMap();
        map.put("name",name);
        map.put("type",type);
        List<Store> stores = storeService.selectByName(map);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");
        jsonResult.setData(stores);

        return  jsonResult.getMap();
    }



    @ApiOperation(value = "路线详情全部删除",produces = "query")
    @PostMapping("/selectDetailAll")
    public Map deleteAll( String name){

       routeDetailService.deleteAll(name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("");

        return  jsonResult.getMap();
    }

}
